<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--搜索药品信息数据表单-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="药品名称" prop="drugName">
            <el-input
              v-model="queryParams.drugName"
              placeholder="请输入药品名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="关键字" prop="drugKey">
            <el-input
              v-model="queryParams.drugKey"
              placeholder="请输入关键字"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!--药品["类型"]下拉菜单-->
          <el-form-item label="药品类型" prop="drugDrugType">
            <el-select
              v-model="queryParams.drugDrugType"
              placeholder="药品类型"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in drugTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <!--药品["生产厂家"]下拉菜单-->
          <el-form-item label="生产厂家" prop="facId">
            <el-select
              v-model="queryParams.facId"
              placeholder="生产厂家"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in factoriesOptions"
                :key="dict.facId"
                :label="dict.facName"
                :value="dict.facId"
              />
            </el-select>
            <!--:label="dict.dictLabel" 是指在界面展示 内容(后台返回值)-->
            <!--:key="dict.dictValue"   是指向后台传   递数据的key-->
            <!--:value="dict.dictValue" 是指想后台传 递的数据-->
          </el-form-item>
          <!--["处方类型"]下拉菜单-->
          <el-form-item label="处方类型" prop="drugRxType">
            <el-select
              v-model="queryParams.drugRxType"
              placeholder="处方类型"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in RxTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <!--药品["状态"]下拉菜单-->
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="药品状态"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['drug:pill:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['drug:pill:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['drug:pill:remove']"
            >删除</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="pills" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="药品ID" align="center" key="drugId" prop="drugId"  />
          <el-table-column label="药品名称" align="center" key="drugName" prop="drugName" :show-overflow-tooltip="true" />
          <el-table-column label="药品编码" align="center" key="drugNumber" prop="drugNumber" :show-overflow-tooltip="true" />
          <el-table-column label="生产厂家" align="center" key="facId" prop="facId" :formatter="getFacNames" :show-overflow-tooltip="true" />
          <el-table-column label="药品类型" align="center" key="drugDrugType" prop="drugDrugType" :formatter="drugDrugTypeFormat" :show-overflow-tooltip="true" />
          <el-table-column label="处方类型" align="center" key="drugRxType" prop="drugRxType" :formatter="drugRxTypeFormat" :show-overflow-tooltip="true" />
          <el-table-column label="单位" align="center" key="drugUnit" prop="drugUnit"  :show-overflow-tooltip="true" />
          <el-table-column label="处方价格" align="center" key="drugRxPrice" prop="drugRxPrice"  :show-overflow-tooltip="true" />
          <el-table-column label="库存量" align="center" key="drugStocks" prop="drugStocks"  :show-overflow-tooltip="true" />
          <el-table-column label="预警值" align="center" key="drugWarning" prop="drugWarning"  :show-overflow-tooltip="true" />
          <el-table-column label="换算量" align="center" key="drugMatrixing" prop="drugMatrixing"  :show-overflow-tooltip="true" />
          <el-table-column label="状态" prop="status" :formatter="statusFormat" width="80"></el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['drug:pill:edit']"
              >修改</el-button>
              <el-button
                v-if="scope.row.drugId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['drug:pill:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="药品名称" prop="drugName">
              <el-input v-model="form.drugName" placeholder="请输入药品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品编号" prop="drugNumber">
              <el-input v-model="form.drugNumber" placeholder="请输入药品编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字" prop="drugKey">
              <el-input v-model="form.drugKey" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="drugUnit">
              <el-input v-model="form.drugUnit" placeholder="请输入单位" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="换算量" prop="drugMatrixing">
              <el-input v-model="form.drugMatrixing" placeholder="请输入换算量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品价格" prop="drugRxPrice">
              <el-input-number v-model="form.drugRxPrice" placeholder="请输入药品价格" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库存量" prop="drugStocks">
              <el-input v-model="form.drugStocks" placeholder="请输入库存量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警值" prop="drugWarning">
              <el-input-number v-model="form.drugWarning" placeholder="请输入药品价格" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="药品类型">
              <el-select v-model="form.drugDrugType" placeholder="请选择">
                <el-option
                  v-for="dict in drugTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处方类型">
              <el-select v-model="form.drugRxType" placeholder="请选择">
                <el-option
                  v-for="dict in RxTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="厂家名称">
              <el-select v-model="form.facId">
                <el-option
                  v-for="dict in factoriesOptions"
                  :key="dict.facId"
                  :label="dict.facName"
                  :value="dict.facId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    </div>
</template>

<script>

import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {allFactories , listDrugs , addDrug , getDrug , updateDrug , delDrugs} from "@/api/drug/pill";

export default {
  name: "index",
  data() {
    return {
      loading: true,// 遮罩层
      ids: [],// 选中数组
      single: true,// 非单个禁用
      multiple: true,// 非多个禁用
      showSearch: true,// 显示搜索条件
      total: 0,// 总条数
      pills: null,// 用户表格数据
      title: "",// 弹出层标题
      dateRange: [],// 日期范围
      deptOptions: undefined,// 部门树选项
      open: false,// 是否显示弹出层
      facBuildTime: [],// 日期范围
      statusOptions: [],// 状态数据字典
      drugTypeOptions: [],//药品类型下拉框选项
      factoriesOptions: [],//生产厂下拉框选项
      RxTypeOptions: [],//处方类型下拉框选项
      sexOptions: [],// 性别状态字典
      postOptions: [],// 岗位选项
      roleOptions: [],// 角色选项
      form: {// 表单参数
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugId:undefined,
        drugRxPrice:undefined,
        drugWarning:undefined,
        drugStocks:undefined,
        drugMatrixing:undefined,
        drugUnit:undefined,
        drugName: undefined,
        drugNumber: undefined,
        drugKey: undefined,
        drugDrugType:undefined,
        facId: undefined,
        drugRxType: undefined,
        status: undefined
      },
      // 表单校验
      rules: {
        drugName: [
          { required: true, message: "药品名称名称不能为空", trigger: "blur" }
        ],
        drugKey: [
          { required: true, message: "关键字不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getFactories()
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    /**获得药品分类的下拉菜单*/
    this.getDicts("drug_pill_type").then(response => {
      this.drugTypeOptions = response.data;
    });
    /**获得处方分类的下拉菜单*/
    this.getDicts("drug_Rx_type").then(response => {
      this.RxTypeOptions = response.data;
    });

    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {

    getList() {
      this.loading = true;
      /**表单展示查询所有药品信息*/
      listDrugs(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.pills = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /**查询所有厂家的id去完成下拉菜单选项*/
    getFactories(){
      allFactories().then(resp=>{
        this.factoriesOptions=resp.data;
      })
    },
    /**厂家名称的字典查询*/
    getFacNames(row,column){
      for (var i in this.factoriesOptions){
        if (this.factoriesOptions[i].facId===row.facId) {
          return this.factoriesOptions[i].facName;
        }
      }
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 菜单状态字典翻译--药品类型
    drugDrugTypeFormat(row, column) {
      return this.selectDictLabel(this.drugTypeOptions, row.drugDrugType);
    },
    // 菜单状态字典翻译--处方类型
    drugRxTypeFormat(row, column) {
      return this.selectDictLabel(this.RxTypeOptions, row.drugRxType);
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    // 生产厂户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.userName + '"生产厂吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeUserStatus(row.userId, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drugId:undefined,
        facId: undefined,
        drugName: undefined,
        drugNumber: undefined,
        drugDrugType: undefined,
        drugRxType: undefined,
        drugUnit: undefined,
        drugRxPrice: undefined,
        drugStocks: undefined,
        drugWarning: undefined,
        drugMatrixing: undefined,
        drugKey: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drugId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drugId = row.drugId || this.ids;
      getDrug(drugId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drugId != undefined) {
            updateDrug(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrug(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const drugIds = row.drugId || this.ids;
      this.$confirm('是否确认删除生产厂编号为"' + drugIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDrugs(drugIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    }
  }
};
</script>
